import { request } from '@/service/request';

/** 数据质量管理 API */

// ==================== 稽查规则 ====================

/** 查询稽查规则列表 */
export function fetchCheckRuleList(params?: Api.DataPlatform.CheckRuleSearchParams) {
  return request<Api.DataPlatform.CheckRuleList>({
    url: '/dataplatform/quality/check-rules',
    method: 'get',
    params
  });
}

/** 获取稽查规则详情 */
export function fetchCheckRuleDetail(id: number) {
  return request<Api.DataPlatform.CheckRuleDetail>({
    url: `/dataplatform/quality/check-rules/${id}`,
    method: 'get'
  });
}

/** 新增稽查规则 */
export function addCheckRule(data: Api.DataPlatform.CheckRuleEdit) {
  return request<number>({
    url: '/dataplatform/quality/check-rules',
    method: 'post',
    data
  });
}

/** 修改稽查规则 */
export function updateCheckRule(data: Api.DataPlatform.CheckRuleEdit) {
  return request({
    url: '/dataplatform/quality/check-rules',
    method: 'put',
    data
  });
}

/** 删除稽查规则 */
export function deleteCheckRule(ids: number[]) {
  return request({
    url: `/dataplatform/quality/check-rules/${ids.join(',')}`,
    method: 'delete'
  });
}

/** 根据维度查询规则 */
export function fetchCheckRulesByDimension(dimension: string) {
  return request<Api.DataPlatform.CheckRule[]>({
    url: `/dataplatform/quality/check-rules/dimension/${dimension}`,
    method: 'get'
  });
}

// ==================== 清洗规则 ====================

/** 查询清洗规则列表 */
export function fetchCleanRuleList(params?: Api.DataPlatform.CleanRuleSearchParams) {
  return request<Api.DataPlatform.CleanRuleList>({
    url: '/dataplatform/quality/clean-rules',
    method: 'get',
    params
  });
}

/** 获取清洗规则详情 */
export function fetchCleanRuleDetail(id: number) {
  return request<Api.DataPlatform.CleanRuleDetail>({
    url: `/dataplatform/quality/clean-rules/${id}`,
    method: 'get'
  });
}

/** 新增清洗规则 */
export function addCleanRule(data: Api.DataPlatform.CleanRuleEdit) {
  return request<number>({
    url: '/dataplatform/quality/clean-rules',
    method: 'post',
    data
  });
}

/** 修改清洗规则 */
export function updateCleanRule(data: Api.DataPlatform.CleanRuleEdit) {
  return request({
    url: '/dataplatform/quality/clean-rules',
    method: 'put',
    data
  });
}

/** 删除清洗规则 */
export function deleteCleanRule(ids: number[]) {
  return request({
    url: `/dataplatform/quality/clean-rules/${ids.join(',')}`,
    method: 'delete'
  });
}

// ==================== 稽查任务 ====================

/** 查询稽查任务列表 */
export function fetchCheckTaskList(params?: Api.DataPlatform.CheckTaskSearchParams) {
  return request<Api.DataPlatform.CheckTaskList>({
    url: '/dataplatform/quality/check-tasks',
    method: 'get',
    params
  });
}

/** 获取稽查任务详情 */
export function fetchCheckTaskDetail(id: number) {
  return request<Api.DataPlatform.CheckTaskDetail>({
    url: `/dataplatform/quality/check-tasks/${id}`,
    method: 'get'
  });
}

/** 新增稽查任务 */
export function addCheckTask(data: Api.DataPlatform.CheckTaskEdit) {
  return request<number>({
    url: '/dataplatform/quality/check-tasks',
    method: 'post',
    data
  });
}

/** 修改稽查任务 */
export function updateCheckTask(data: Api.DataPlatform.CheckTaskEdit) {
  return request({
    url: '/dataplatform/quality/check-tasks',
    method: 'put',
    data
  });
}

/** 删除稽查任务 */
export function deleteCheckTask(ids: number[]) {
  return request({
    url: `/dataplatform/quality/check-tasks/${ids.join(',')}`,
    method: 'delete'
  });
}

/** 执行稽查任务 */
export function executeCheckTask(id: number) {
  return request<Api.DataPlatform.CheckResult>({
    url: `/dataplatform/quality/check-tasks/${id}/execute`,
    method: 'post'
  });
}

// ==================== 清洗任务 ====================

/** 查询清洗任务列表 */
export function fetchCleanTaskList(params?: Api.DataPlatform.CleanTaskSearchParams) {
  return request<Api.DataPlatform.CleanTaskList>({
    url: '/dataplatform/quality/clean-tasks',
    method: 'get',
    params
  });
}

/** 获取清洗任务详情 */
export function fetchCleanTaskDetail(id: number) {
  return request<Api.DataPlatform.CleanTaskDetail>({
    url: `/dataplatform/quality/clean-tasks/${id}`,
    method: 'get'
  });
}

/** 新增清洗任务 */
export function addCleanTask(data: Api.DataPlatform.CleanTaskEdit) {
  return request<number>({
    url: '/dataplatform/quality/clean-tasks',
    method: 'post',
    data
  });
}

/** 修改清洗任务 */
export function updateCleanTask(data: Api.DataPlatform.CleanTaskEdit) {
  return request({
    url: '/dataplatform/quality/clean-tasks',
    method: 'put',
    data
  });
}

/** 删除清洗任务 */
export function deleteCleanTask(ids: number[]) {
  return request({
    url: `/dataplatform/quality/clean-tasks/${ids.join(',')}`,
    method: 'delete'
  });
}

/** 执行清洗任务 */
export function executeCleanTask(id: number) {
  return request<Api.DataPlatform.CleanResult>({
    url: `/dataplatform/quality/clean-tasks/${id}/execute`,
    method: 'post'
  });
}

/** 回滚清洗任务 */
export function rollbackCleanTask(id: number) {
  return request({
    url: `/dataplatform/quality/clean-tasks/${id}/rollback`,
    method: 'post'
  });
}

// ==================== 质量报告 ====================

/** 查询质量报告列表 */
export function fetchQualityReportList(params?: Api.DataPlatform.QualityReportSearchParams) {
  return request<Api.DataPlatform.QualityReportList>({
    url: '/dataplatform/quality/reports',
    method: 'get',
    params
  });
}

/** 获取质量报告详情 */
export function fetchQualityReportDetail(id: number) {
  return request<Api.DataPlatform.QualityReportDetail>({
    url: `/dataplatform/quality/reports/${id}`,
    method: 'get'
  });
}

/** 获取质量趋势 */
export function fetchQualityTrend(tableId: number, startDate?: string, endDate?: string) {
  return request<Api.DataPlatform.QualityTrend>({
    url: `/dataplatform/quality/reports/trend/${tableId}`,
    method: 'get',
    params: { startDate, endDate }
  });
}

/** 获取维度统计 */
export function fetchDimensionStats(tableId: number) {
  return request<Record<string, any>>({
    url: `/dataplatform/quality/reports/dimension-stats/${tableId}`,
    method: 'get'
  });
}
